package com.guyan.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guyan.entity.Order;
import com.guyan.mapper.OrderMapper;
import com.guyan.service.OrderService;
import com.guyan.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.UUID;

/**
 * ClassName： OrderServiceImpl
 * Date： 2021/6/15 20:53
 *
 * @author 孤焰
 * @version 1.0
 **/

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public PageInfo orderList(Integer page, Integer limit, Order order, Date startDate, Date endDate) {
        PageHelper.startPage(page, limit);
        PageInfo pageInfo = new PageInfo(orderMapper.orderList(order, startDate, endDate));
        return pageInfo;
    }

    @Override
    public Order getOrderById(Integer id) {
        return orderMapper.getOrderById(id);
    }

    @Override
    public ResultVo addOrder(Order order) {
        Date date = new Date();
        String orderNo = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 20).toUpperCase();
        order.setOrderNo(orderNo);
        order.setCreateTime(date);
        order.setUpdateTime(date);
        return orderMapper.addOrder(order) > 0 ?
                ResultVo.success() : ResultVo.error(6000, order+"数据库中插入数据失败");
    }

    @Override
    public ResultVo updateOrderById(Order order) {
        Date date = new Date();

        if (order.getStatus() != null) {
            switch (order.getStatus()) {
                case 2:
                    order.setPaymentTime(date);
                    break;
                case 3:
                    order.setSendTime(date);
                    break;
                case 4:
                    order.setEndTime(date);
                    break;
                case 5:
                    order.setCloseTime(date);
            }
        }

        order.setUpdateTime(date);
        return orderMapper.updateOrderById(order) > 0 ?
                ResultVo.success() : ResultVo.error(6000, order+"数据库中更新数据失败");
    }

    @Override
    public ResultVo batchDelOrder(Integer[] ids) {
        return orderMapper.batchDelOrder(ids) > 0 ?
                ResultVo.success() : ResultVo.error(6000, "数据库层批量删除失败");
    }
}
